<?php

class Exam_Model_DbTable_Exams extends Engine_Db_Table
{
  protected $_rowClass = 'Exam_Model_Exam';

  
  public function getExamSelect($options = array())
  {
    $select = $this->select();
	//to do
	
    return $select;
  }

  public function getExamPaginator($options = array())
  {
    return Zend_Paginator::factory($this->getExamSelect($options));
  }
  
  public function getSelectHistoryTest($user_id,$options){
        $tT = Engine_Api::_()->getDbTable('tests','exam');
        $tN = $tT->info('name');
        $sl = $this->select()
                   ->setIntegrityCheck(false)
                   ->from(array('exams' => $this->info('name')),array('*'))
                   ->join(array('tests'=>$tN),
                        'exams.exam_id = tests.exam_id',
                        (array(new Zend_Db_Expr('max(total_score) as max_score, min(total_score) as min_score, count(*) as total_test, max(start_time) as recent_date'),''))
                   ) 
                   ->where('tests.owner_id = ?',$user_id)
                   ->group('exams.exam_id')
                   ->order($options['order'].' '.$options['direction']);
        if(isset($options['title']) && !empty($options['title'])){
            $sl->where('exams.title like ?','%'.$options['title'].'%');
        }
        if(isset($options['category_id']) && !empty($options['category_id'])){
            $sl->where('exams.category_id = ?',$options['category_id']);
        }
        if(isset($options['starttime']) && !empty($options['starttime'])){
            $sl->where('tests.start_time >= ?',$options['starttime']);
        }
        if(isset($options['endtime']) && !empty($options['endtime'])){
            $sl->where('tests.end_time <= ?',$options['endtime']);
        }
        
        return $sl;
  }
}